package com.bjpowernode.app.dao;

import com.bjpowernode.app.beans.ClassRoom;
import com.bjpowernode.app.exception.MyException;

import java.util.ArrayList;
import java.util.Iterator;

public class ClassRoomDaoImp implements ClassRoomDao {
    @Override
    public ArrayList<ClassRoom> getAll() {
        return DB.classRooms;
    }

    @Override
    public ClassRoom get(int id) {


        for (ClassRoom classRoom : DB.classRooms) {
            if (classRoom.getId() == id) {
                return classRoom;
            }
        }

        return null;
    }

    @Override
    public void add(ClassRoom classRoom) throws MyException {
        boolean add = DB.classRooms.add(classRoom);
        if (!add) {
            throw new MyException("dao层添加班级失败...");
        }
    }

    @Override
    public void edit(ClassRoom classRoom) throws MyException {

        for (ClassRoom classRoomTemp : DB.classRooms) {
            if (classRoomTemp.getId() == classRoom.getId()) {
                classRoomTemp.setName(classRoom.getName());
                return;
            }
        }
        throw new MyException("未找到【"+classRoom.getId()+"】的班级，dao层修改班级失败");

    }
    @Override
    public void del(int id) throws MyException {

        Iterator<ClassRoom> iterator = DB.classRooms.iterator();
        while (iterator.hasNext()){
            ClassRoom classRoom = iterator.next();
            if(classRoom.getId()==id){
                iterator.remove();
                return;
            }
        }
        throw new MyException("未找到【"+id+"】的班级，dao层删除班级失败");
    }
}
